package com.henanuniversity.dao.impl;

import com.henanuniversity.dao.UserDao;
import com.henanuniversity.po.User;
import com.henanuniversity.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    @Override
    public int saveUser(User user) {
        int result=0;
        String sql="insert into user values(?,?,?,?,?,1)";
        try {
            conn = DBUtils.getConnection();
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,user.getUserId());
            pst.setString(2,user.getPassword());
            pst.setString(3,user.getUserName());
            pst.setInt(4,user.getUserSex());
            pst.setString(5,user.getUserImg());
            System.out.println(pst.toString());
            result= pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);
        }
        return result;
    }

    @Override
    public User getUserByIdByPass(String userId, String password) {
        User user=null;
        String sql="select * from user where userId=? and password=?";
        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setString(1,userId);
            pst.setString(2,password);
            rs=pst.executeQuery();
            if(rs.next()){
                user=new User();
                user.setUserId(rs.getString("userId"));
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setUserSex(rs.getInt("userSex"));
                user.setUserImg(rs.getString("userImg"));
                user.setDelTag(rs.getInt("delTag"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);
        }
        return user;
    }

    @Override
    public User getUserById(String userId) {
        User user=null;
        String sql="select * from user where userId=?";
        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setString(1,userId);
            rs=pst.executeQuery();
            if(rs.next()){
                user=new User();
                user.setUserId(rs.getString("userId"));
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setUserSex(rs.getInt("userSex"));
                user.setUserImg(rs.getString("userImg"));
                user.setDelTag(rs.getInt("delTag"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);
        }
        return user;
    }
}
